﻿using Microsoft.EntityFrameworkCore;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using WisDom.SmartTMS.Entity;
using WisDom.SmartTMS.Validator;

namespace WisDom.SmartTMS.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class SmartTMSDbContext : 
        AbpDbContext<SmartTMSDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */
        
        #region Entities from the modules
        
        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        
        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        #endregion
        
        public SmartTMSDbContext(DbContextOptions<SmartTMSDbContext> options)
            : base(options)
        {
            
        }

        //人事模块
        public DbSet<PersonnelRegister> PersonnelRegisters { get; set; }//人事登记
        public DbSet<LeaveDeal> LeaveDeals { get; set; }//离职办理
        public DbSet<PersonnelEntry> PersonnelEntrys { get; set; }//人事入职
        public DbSet<PositiveDeal> PositiveDeals { get; set; }//人事入职

        public DbSet<Audit> Audit { get; set; }//审核
        public DbSet<AuditLog> AuditLog { get; set; }//审核入职

        public DbSet<TMSContractCarrier> TMSContractCarrier { get; set; }//货主合同

        public DbSet<Users> User { get; set; }//用户
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(SmartTMSConsts.DbTablePrefix + "YourEntities", SmartTMSConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});
            //#region 转正办理
            //builder.Entity<PositiveDeal>(b =>
            //{
            //    b.ToTable("PositiveDeal");
            //    b.Property("
            //    ").HasMaxLength(50).IsRequired();
            //    b.Property("PosID").HasMaxLength(50).IsRequired();
            //    b.Property("Position").HasMaxLength(50).IsRequired();
            //    b.Property("Principal").HasMaxLength(50).IsRequired();
            //    b.Property("APospprovalStatus").HasMaxLength(50).IsRequired();

            //    b.Property("PosEntryTimes").HasColumnType("date").IsRequired();
            //    b.Property("PosApply").HasColumnType("date").IsRequired();
            //    b.Property("CreateTimes").HasColumnType("date").IsRequired();
            //});
            //#endregion

            #region 映射配置规则
            //应用VehicleMap映射配置类（此处演示将映射类写到一个单独的文件，方便维护）
            //人事登记
            builder.ApplyConfiguration(new PersonnelRegisterValidator());
            //离职办理
            builder.ApplyConfiguration(new LeaveDealValidator());
            //人事入职
            builder.ApplyConfiguration(new PersonnelEntryValidator());
            //转正办理
            builder.ApplyConfiguration(new PositiveDealValidator());
            //用户
            builder.ApplyConfiguration(new UsersValidator());
            #endregion
        }
    }
}
